package org.argus.job.server.remote.worker.selector;

import java.util.List;
import java.util.Map;

import org.argus.job.common.enums.DispatchStrategy;
import org.argus.job.common.model.InstanceInfo;
import org.argus.job.common.model.JobInfo;
import org.argus.job.server.common.model.WorkerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

/**
 * TaskTrackerSelectorService
 *
 * @author tjq
 * @since 2024/2/24
 */
@Service
public class TaskTrackerSelectorService {

    private final Map<DispatchStrategy, TaskTrackerSelector> taskTrackerSelectorMap = Maps.newHashMap();

    @Autowired
    public TaskTrackerSelectorService(List<TaskTrackerSelector> taskTrackerSelectors) {
        taskTrackerSelectors.forEach(ts -> taskTrackerSelectorMap.put(ts.strategy(), ts));
    }

    public WorkerInfo select(JobInfo jobInfo, InstanceInfo instanceInfo, List<WorkerInfo> availableWorkers) {
        TaskTrackerSelector taskTrackerSelector = taskTrackerSelectorMap.get(jobInfo.getDispatchStrategy());
        return taskTrackerSelector.select(jobInfo, instanceInfo, availableWorkers);
    }
}
